package com.swensen.game.editor;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import com.swensen.game.data.GameState;
import com.swensen.game.data.Tile;
import com.swensen.game.data.Tile.TileId;
import com.swensen.game.data.Tile.TileType;
import com.swensen.game.editor.data.EditorBoardData;

/**
 * The Class TileSelector.
 */
public class TileSelector extends JPanel implements MouseListener {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -3978242817225736311L;

	/**
	 * Instantiates a new tile selector.
	 */
	public TileSelector() {
		setFocusable(true);
		setBackground(Color.black);
		setBorder(BorderFactory.createLoweredBevelBorder());
		setDoubleBuffered(true);
		setPreferredSize(new Dimension(3 * Tile.TILE_SIZE + 1, 10 * Tile.TILE_SIZE));
		setMaximumSize(getPreferredSize());
		setMinimumSize(getPreferredSize());
		
		addMouseListener(this);
	}
	
	/**
	 * Draw tiles.
	 *
	 * @param g the g
	 */
	private void drawTiles(Graphics2D g) {
		int width = (getWidth() / Tile.TILE_SIZE) * Tile.TILE_SIZE;
		if (width < Tile.TILE_SIZE) {
			return;
		}
		
		for (int i = 0; i < GameState.getTiles().size(); ++i) {
			Tile t = GameState.getTiles().get(i);
			TileId imageData;
			BufferedImage image;
			if (t == null ||
					(imageData = t.getStillImageData()) == null ||
					(image = GameState.getCurrentTileSet().getImage(imageData)) == null) {
				continue;
			}
			
			int x = ((i + 1) * Tile.TILE_SIZE) % width;
			int y = (((i + 1) * Tile.TILE_SIZE) / width) * Tile.TILE_SIZE + yOffset; 
			g.drawImage(image, x, y, image.getWidth(), image.getHeight(), null);
		}
		
		int i = GameState.INSTANCE.<EditorBoardData>getBoardData().getCurrentTileNumber();
		Tile current = GameState.INSTANCE.<EditorBoardData>getBoardData().getCurrentTile();
		TileType type = current != null ? current.getType() : TileType.BASIC;
		int x = ((i + 1) * Tile.TILE_SIZE) % width;
		int y = (((i + 1) * Tile.TILE_SIZE) / width) * Tile.TILE_SIZE + yOffset;
		
		Color color = g.getColor();
		switch (type) {
		case ANIMATED:
			g.setColor(Color.CYAN);
			break;
		case BASIC:
			g.setColor(Color.GREEN);
			break;
		case FILL:
			g.setColor(Color.MAGENTA);
			break;
		default:
			break;
		}
		
		
		g.drawRect(x, y, Tile.TILE_SIZE, Tile.TILE_SIZE);
		g.setColor(color);
	}
	
	int yOffset = 0;
	private void setOffset() {
		int width = (getWidth() / Tile.TILE_SIZE) * Tile.TILE_SIZE;
		int i = GameState.INSTANCE.<EditorBoardData>getBoardData().getCurrentTileNumber();
		int y = (((i + 1) * Tile.TILE_SIZE) / width) * Tile.TILE_SIZE;
		
		if (y < Math.abs(yOffset)) {
			yOffset = -y;
			return;
		}
		
		int max = getHeight();
		if ((y + (Tile.TILE_SIZE)) + yOffset > max) {
			yOffset = getHeight() - (y + Tile.TILE_SIZE);
		}
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent e) {
		int x = e.getX() / Tile.TILE_SIZE;
		int y = (e.getY() / Tile.TILE_SIZE) + yOffset;
		int w = getWidth() / Tile.TILE_SIZE;
		GameState.INSTANCE.<EditorBoardData>getBoardData().setCurrentTile((x + (y * w)) - 1);
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent e) {

	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent e) {

	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent e) {

	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent e) {

	}

	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paint(java.awt.Graphics)
	 */
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		setOffset();
		drawTiles((Graphics2D)g);
		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}
	
}
